# Maintainer: Felix Yan <felixonmars@archlinux.org>

_hkgname=hie-bios
pkgname=haskell-hie-bios
pkgver=0.11.0
pkgrel=60
pkgdesc="Set up a GHC API session"
url="https://github.com/mpickering/hie-bios"
license=("BSD")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-aeson' 'haskell-base16-bytestring' 'haskell-co-log-core'
         'haskell-conduit' 'haskell-conduit-extra' 'haskell-cryptohash-sha1' 'haskell-exceptions'
         'haskell-extra' 'haskell-file-embed' 'haskell-optparse-applicative'
         'haskell-prettyprinter' 'haskell-temporary' 'haskell-unix-compat'
         'haskell-unordered-containers' 'haskell-vector' 'haskell-yaml')
makedepends=('ghc' 'uusi' 'haskell-tagged' 'haskell-tasty' 'haskell-tasty-expected-failure'
             'haskell-tasty-hunit')
checkdepends=('cabal-install')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha512sums=('51301be68ece83c57a474c669a5952267332865c037eb8b1561939884ace587903e68dce1a5caac4da34d4020d95e31a8f457f26cb3634331c5e065bdb3f62e4')

prepare() {
  cd $_hkgname-$pkgver
  uusi -u unix-compat -u vector $_hkgname.cabal

  # https://github.com/mpickering/hie-bios/issues/290
  chmod +x tests/projects/*/*.sh
}

build() {
  cd $_hkgname-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie'

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd $_hkgname-$pkgver
  cabal update  # Generate cabal config for tests
  # TODO
  runhaskell Setup test --show-details=direct || echo "Tests failed"
}

package() {
  cd $_hkgname-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
